package com.cskaoyan.service;

import com.cskaoyan.bean.config.ExpressConfig;
import com.cskaoyan.bean.config.MallConfig;
import com.cskaoyan.bean.config.OrderConfig;
import com.cskaoyan.bean.config.WxConfig;
import org.apache.ibatis.io.Resources;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Properties;

/**
 * @description:
 * @author:
 * @date:
 */
@Service
public class ConfigServiceImpl implements ConfigService {
    Properties properties;
    public ConfigServiceImpl() {
        this.properties = new Properties();
        InputStream inputStream = null;
        try {
            //在加载组件的时候完成配置文件的读取
            inputStream = Resources.getResourceAsStream("config.properties");
            properties.load(new InputStreamReader(inputStream,"utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public MallConfig mall() {
        String market_mall_address = properties.getProperty("market_mall_address");
        String market_mall_latitude = properties.getProperty("market_mall_latitude");
        String market_mall_longitude = properties.getProperty("market_mall_longitude");
        String market_mall_name = properties.getProperty("market_mall_name");
        String market_mall_phone = properties.getProperty("market_mall_phone");
        String market_mall_qq = properties.getProperty("market_mall_qq");
        MallConfig mallInstance = MallConfig.getInstance(market_mall_address, market_mall_latitude, market_mall_longitude, market_mall_name, market_mall_phone, market_mall_qq);
        return mallInstance;
    }

    @Override
    public ExpressConfig express() {
        String market_express_freight_min = properties.getProperty("market_express_freight_min");
        String market_express_freight_value = properties.getProperty("market_express_freight_value");
        ExpressConfig expressInstance = ExpressConfig.getInstance(market_express_freight_min, market_express_freight_value);
        return expressInstance;
    }

    @Override
    public OrderConfig order() {
        String market_order_comment = properties.getProperty("market_order_comment");
        String market_order_unconfirm = properties.getProperty("market_order_unconfirm");
        String market_order_unpaid = properties.getProperty("market_order_unpaid");
        OrderConfig orderInstance = OrderConfig.getInstance(market_order_comment, market_order_unconfirm, market_order_unpaid);
        return orderInstance;
    }

    @Override
    public WxConfig wx() {
        String market_wx_catlog_goods = properties.getProperty("market_wx_catlog_goods");
        String market_wx_catlog_list = properties.getProperty("market_wx_catlog_list");
        String market_wx_index_brand = properties.getProperty("market_wx_index_brand");
        String market_wx_index_hot = properties.getProperty("market_wx_index_hot");
        String market_wx_index_new = properties.getProperty("market_wx_index_new");
        String market_wx_index_topic = properties.getProperty("market_wx_index_topic");
        String market_wx_share = properties.getProperty("market_wx_share");
        WxConfig wxInstance = WxConfig.getInstance(market_wx_catlog_goods, market_wx_catlog_list, market_wx_index_brand, market_wx_index_hot, market_wx_index_new, market_wx_index_topic, market_wx_share);
        return wxInstance;
    }

    @Override
    public MallConfig mallUpdate(MallConfig mallConfig) {
        Writer writer = null;
        try {
            properties.setProperty("market_mall_address", mallConfig.getMarket_mall_address());
            properties.setProperty("market_mall_latitude", mallConfig.getMarket_mall_latitude());
            properties.setProperty("market_mall_longitude", mallConfig.getMarket_mall_longitude());
            properties.setProperty("market_mall_name", mallConfig.getMarket_mall_name());
            properties.setProperty("market_mall_phone", mallConfig.getMarket_mall_phone());
            properties.setProperty("market_mall_qq", mallConfig.getMarket_mall_qq());
            writer = new OutputStreamWriter(new FileOutputStream(new File("src/main/resources/config.properties")),"utf-8");
            properties.store(writer,"mall update");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("商场配置更改失败");
        }finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return mallConfig;
    }

    @Override
    public ExpressConfig expressUpdate(ExpressConfig expressConfig) {
        Writer writer = null;
        try {
            properties.setProperty("market_express_freight_min", expressConfig.getMarket_express_freight_min());
            properties.setProperty("market_express_freight_value", expressConfig.getMarket_express_freight_value());
            writer = new OutputStreamWriter(new FileOutputStream(new File("src/main/resources/config.properties")), "utf-8");
            properties.store(writer, "express update");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("运费配置更改失败");
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return expressConfig;
    }

    @Override
    public OrderConfig orderUpdate(OrderConfig orderConfig) {
        Writer writer = null;
        try {
            properties.setProperty("market_order_comment", orderConfig.getMarket_order_comment());
            properties.setProperty("market_order_unconfirm", orderConfig.getMarket_order_unconfirm());
            properties.setProperty("market_order_unpaid", orderConfig.getMarket_order_unpaid());
            writer = new OutputStreamWriter(new FileOutputStream(new File("src/main/resources/config.properties")), "utf-8");
            properties.store(writer, "order update");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("订单配置更改失败");
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return orderConfig;
    }

    @Override
    public WxConfig wxUpdate(WxConfig wxConfig) {
        Writer writer = null;
        try {
            properties.setProperty("market_wx_catlog_goods", wxConfig.getMarket_wx_catlog_goods());
            properties.setProperty("market_wx_catlog_list", wxConfig.getMarket_wx_catlog_list());
            properties.setProperty("market_wx_index_brand", wxConfig.getMarket_wx_index_brand());
            properties.setProperty("market_wx_index_hot", wxConfig.getMarket_wx_index_hot());
            properties.setProperty("market_wx_index_new", wxConfig.getMarket_wx_index_new());
            properties.setProperty("market_wx_index_topic", wxConfig.getMarket_wx_index_topic());
            properties.setProperty("market_wx_share", wxConfig.getMarket_wx_share());
            writer = new OutputStreamWriter(new FileOutputStream(new File("src/main/resources/config.properties")), "utf-8");
            properties.store(writer, "wx update");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("小程序配置更改失败");
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return wxConfig;
    }
}
